﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_Model;
using V5_Common;
using System.Data;
using V5_DAL.SqlServer;

namespace V5_BLL {
    public class BLLAdmin {
        DALAdmin dal = new DALAdmin();

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelAdmin model) {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelAdmin model) {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID) {

            dal.Delete(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelAdmin GetModel(int ID) {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelAdmin GetModelByCache(int ID) {

            string CacheKey = "ModelAdminModel-" + ID;
            object objModel = DataCacheHelper.GetCache(CacheKey);
            if (objModel == null) {
                try {
                    objModel = dal.GetModel(ID);
                    if (objModel != null) {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCacheHelper.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelAdmin)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelAdmin> GetModelList(string strWhere) {
            DataSet ds = dal.GetList(strWhere);
            List<ModelAdmin> modelList = new List<ModelAdmin>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0) {
                ModelAdmin model;
                for (int n = 0; n < rowsCount; n++) {
                    model = new ModelAdmin();
                    if (ds.Tables[0].Rows[n]["ID"].ToString() != "") {
                        model.ID = int.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    }
                    model.AdminName = ds.Tables[0].Rows[n]["AdminName"].ToString();
                    model.AdminPwd = ds.Tables[0].Rows[n]["AdminPwd"].ToString();
                    if (ds.Tables[0].Rows[n]["LoginTimes"].ToString() != "") {
                        model.LoginTimes = int.Parse(ds.Tables[0].Rows[n]["LoginTimes"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["LoginTime"].ToString() != "") {
                        model.LoginTime = DateTime.Parse(ds.Tables[0].Rows[n]["LoginTime"].ToString());
                    }
                    model.LoginIP = ds.Tables[0].Rows[n]["LoginIP"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList() {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return dal.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }


        public bool ChkAdminLogin(ref ModelAdmin model) {
            DataSet ds = this.GetList(string.Format(" And AdminName='{0}' and AdminPwd='{1}' ", model.AdminName, model.AdminPwd));
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                //更新信息
                model.ID = InputHelper.SetNumber(ds.Tables[0].Rows[0]["ID"].ToString());
                this.UpdateAdminInfo(model);
                return true;
            }
            return false;
        }
        //更新管理员信息
        private void UpdateAdminInfo(ModelAdmin model) {
            ModelAdmin smodel = this.GetModel(model.ID);
            if (smodel != null) {
                smodel.LoginTimes = model.LoginTimes + 1;
                smodel.LoginTime = model.LoginTime;
                smodel.LoginIP = model.LoginIP;
                this.Update(smodel);
            }
        }
        #endregion  成员方法
    }
}
